﻿namespace WebPacker.Compressors
{
	using System.IO;
	using System.IO.Compression;
	using System.Text;

	public sealed class GZipCompressor : ICompressor
	{
		public GZipCompressor()
		{
			Encoding = Encoding.UTF8;
		}

		public string Name
		{
			get { return "gzip"; }
		}

		public Encoding Encoding { get; set; }

		public bool Accept(string acceptEncoding)
		{
			return !string.IsNullOrEmpty(acceptEncoding) && acceptEncoding.Contains("gzip");
		}

		public void Compress(string content, Stream targetStream)
		{
			var bytes = Encoding.GetBytes(content);

			using (var writer = new GZipStream(targetStream, CompressionMode.Compress, true))
			{
				writer.Write(bytes, 0, bytes.Length);
			}
		}
	}
}